package com.dragon.logical.service.avttendance.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.logical.service.avttendance.IArrangePersonnelService;
import com.dragon.repository.entity.ArrangePersonnel;
import com.dragon.repository.mapper.AdminDepartmentMapper;
import com.dragon.repository.mapper.ArrangePersonnelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 考勤配置人员关联 服务实现类
 * </p>
 *
 * @author sy
 * @since 2023-07-31
 */
@Service
public class ArrangePersonnelServiceImpl extends ServiceImpl<ArrangePersonnelMapper, ArrangePersonnel> implements IArrangePersonnelService {

    @Autowired
    ArrangePersonnelMapper perMapper;

    @Autowired
    AdminDepartmentMapper departmentMapper;

    @Override
    public List<ArrangePersonnel> personnelLis(Integer arrangeId) {
        QueryWrapper<ArrangePersonnel> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(arrangeId != null ,ArrangePersonnel::getArrangeId,arrangeId);
        List<ArrangePersonnel> personnels = perMapper.selectList(wrapper);
        return personnels;
    }

    @Override
    public void personnelAdd(Integer arrangeId,ArrangePersonnel personnel){

        List<ArrangePersonnel> per = perMapper.selectList(new QueryWrapper<ArrangePersonnel>().lambda()
                .eq(ArrangePersonnel::getArrangeId, arrangeId));
        if (per.size() == 0 || per == null) {
            personnel.setArrangeId(arrangeId);
            personnel.setAdminId(personnel.getAdminId());
            perMapper.insert(personnel);
        }
        for (ArrangePersonnel arrangePersonnel : per) {
            if (personnel.getAdminId().equals(arrangePersonnel.getAdminId())){
                perMapper.deleteById(arrangePersonnel);
                personnel.setArrangeId(arrangeId);
                personnel.setAdminId(personnel.getAdminId());
                perMapper.insert(personnel);
            }
        }
        // 根据部门添加人员
        //String adminIds = perMapper.selectAdminIds(personnel.getDepartmentId());
//        String departmentIds = perMapper.selectDepartmentIds(personnel.getAdminId());
//        System.out.println(departmentIds);


    }

    @Override
    public void personnelDelete(Integer id){
        perMapper.deleteById(id);
    }
}
